package com.wss.common.wechat.spi.app.event.handler;

import com.google.gson.Gson;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import com.wss.common.context.util.HttpRequestProxyUtil;
import com.wss.common.context.util.SystemConfig;
import com.wss.common.wechat.cache.WeChatCache;
import com.wss.common.wechat.massage.app.AuthInputMessage;
import com.wss.common.wechat.massage.app.TicketInputMessage;
import com.wss.common.wechat.massage.app.UnAuthInputMessage;
import com.wss.common.wechat.massage.app.UpAuthInputMessage;
import com.wss.common.wechat.model.AppAuthInfoModel;
import com.wss.common.wechat.service.AppAuthService;
import com.wss.common.wechat.spi.app.event.IAppEventMessageHandler;
import com.wss.common.wechat.util.OpenWeChatInterfaceUtil;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * @author 赵飞
 * @version V1.0
 * @Title: 应用级微信消息处理
 * @Package com.wss.common.wechat.spi.impl
 * @Description:
 * @date 2017/9/5
 */
@Component
public class AppEventMessageHandler implements IAppEventMessageHandler {

	@Autowired
	private AppAuthService appAuthService;

	private static Logger logger = Logger.getLogger(AppEventMessageHandler.class);

	@Override
	public void ticketTypeMsg(TicketInputMessage msg) {
		//将ticket 缓存
		logger.info("根据微信推送 COMPONENT_VERIFY_TICKET ："+msg.getComponentVerifyTicket());
		WeChatCache.setComponentVerifyTicket(msg.getComponentVerifyTicket());
		logger.info("根据微信推送 COMPONENT_VERIFY_TICKET 放入缓存对象："+WeChatCache.getComponentVerifyTicket());

	}

	@Override
	public void authTypeMsg(AuthInputMessage msg) {
		//获取公众号授权
		JsonParser jsonParser = new JsonParser();
		Gson gson = new Gson();
		JsonObject jsonParam = new JsonObject();
		jsonParam.addProperty("component_appid", SystemConfig.getComponentAppId());
		jsonParam.addProperty("authorization_code", msg.getAuthorizationCode());
		String jsonStr = null;
		try {
			jsonStr = HttpRequestProxyUtil.executeJsonParamHttpPost(OpenWeChatInterfaceUtil.getApiQueryAuth(), jsonParam.toString());
		} catch (Exception e) {
			logger.info("调用微信开放平台获取公众号授权信息接口报错！",e);
		}
		logger.info("获取授权返回：" + jsonStr);
		if (jsonStr != null && !"".equals(jsonStr)) {
			JsonObject jsonObject = jsonParser.parse(jsonStr).getAsJsonObject();
			if (jsonObject.has("authorization_info")) {
				AppAuthInfoModel appAuthInfoModel = gson.fromJson(jsonObject.get("authorization_info").getAsJsonObject(), AppAuthInfoModel.class);
				//将用于刷新公众号授权信息的 refresh_token 存入数据库
				boolean b=false;
				try {
					b = appAuthService.saveAppAuthInfo(appAuthInfoModel);
				}catch (Exception e){
					logger.error("保存公众号授权结果出错！",e);
				}

				logger.info("保存微信公众号授权结果："+b);
				if(b){
					//将授权获取的access_token放入缓存
					WeChatCache.getAppAuthMap().put(appAuthInfoModel.getAuthorizerAppId(), appAuthInfoModel);
				}
			}
		}

	}

	@Override
	public void upAuthTypeMsg(UpAuthInputMessage msg) {
		//授权信息更新
	}

	@Override
	public void unAuthTypeMsg(UnAuthInputMessage msg) {
		//取消授权
	}
}
